package com.woniu.pay.controller;

import com.woniu.common.constant.Constants;
import com.woniu.pay.service.FinanceService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class AlipayController {





    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Autowired
    private FinanceService financeService;


    @RequestMapping("finance/pay")
    public String alipay(Long orderId,Long money, HttpServletRequest req){

        String form = financeService.alipay(orderId,money);
        req.setAttribute("form",form);
        return "alipay";
    }
    @RequestMapping("finance/alipay/notice")
    @ResponseBody
    public void notice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //支付宝支付后回调传的订单号
        String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        System.out.println("支付成功，支付宝回调的订单号："+out_trade_no);
        //发消息给订单微服务，支付已经成功
        Map<String,Long> msg = new HashMap<>();
        msg.put("orderId",Long.valueOf(out_trade_no));
        rabbitTemplate.convertAndSend(Constants.MQ_ORDER_PAY_EXCHANGE_NAME,Constants.MQ_ORDER_PAY_ROUTING_KEY,msg);


//        return AjaxResult.success();
        response.sendRedirect("http://www.wncinema.com");
    }
}
